package com.yingma.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yingma.system.dao.*;
import com.yingma.system.entity.JcCustomer;
import com.yingma.system.entity.JcRegistration;
import com.yingma.system.entity.JcSysOrganization;
import com.yingma.system.service.JcCustomerService;
import com.yingma.system.dao.JcCustomerDao;
import com.yingma.system.entity.JcCustomer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (JcCustomer)表服务实现类
 *
 * @author makejava
 * @since 2020-03-03 16:19:28
 */
@Service("jcCustomerService")
public class JcCustomerServiceImpl implements JcCustomerService {
    @Resource
    private JcCustomerDao jcCustomerDao;

    @Resource
    private JcSysOrganizationDao jcSysOrganizationDao;

    @Resource
    private JcRegistrationDao jcRegistrationDao;

    @Resource
    private JcZoneDao jcZoneDao;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JcCustomer queryById(String id) {
        return this.jcCustomerDao.queryById(id);
    }

    /**
     * 查询全部客户档案信息
     * @param organziation
     * @param customerName
     * @param pageNum
     * @param PageSize
     * @return
     */
    @Override
    public List<JcCustomer> selectList(String organziation,String customerName,Integer pageNum,Integer PageSize) {
        List<JcCustomer> list=null;
        QueryWrapper<JcCustomer> queryWrapper=new QueryWrapper<JcCustomer>();
        if(!organziation.equals("")&&organziation!=""){
            //如果前台传值为组织机构 根据组织机构查询id
            LambdaQueryWrapper<JcSysOrganization> lambdaQuery= Wrappers.<JcSysOrganization>lambdaQuery();
            lambdaQuery.like(JcSysOrganization::getName,organziation);
            List<JcSysOrganization> lists=jcSysOrganizationDao.selectList(lambdaQuery);
            for (int i = 0; i <lists.size() ; i++) {
                queryWrapper.like("JC_ORGANIZATION_ID",lists.get(i).getId()).or();
            }
        }
        if(!customerName.equals("")&&customerName!=""){
            queryWrapper.like("NAME",customerName);
        }
        Page<JcCustomer> page=new Page<>(pageNum,PageSize,true);
        IPage<JcCustomer> iPages=jcCustomerDao.selectPage(page,queryWrapper);
        list=iPages.getRecords();
        //组织机构
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setJcSysOrganization(jcSysOrganizationDao.selectById(list.get(i).getJcOrganizationId()));
        }
        //所属注册公司
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setJcRegistrations(jcRegistrationDao.selectById(list.get(i).getJcRegistration()));
        }
        //行政区域
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setJcZone(jcZoneDao.selectById(list.get(i).getJcZoneId()));

        }

        if(list.size()>0){
            list.get(0).setTotalpageNum(new Long(iPages.getPages()).intValue());
            list.get(0).setTotalSize(new Long(iPages.getTotal()).intValue());
        }
        return list;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<JcCustomer> queryAllByLimit(int offset, int limit) {
        return this.jcCustomerDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param jcCustomer 实例对象
     * @return 实例对象
     */
    @Override
    public JcCustomer insert(JcCustomer jcCustomer) {
        this.jcCustomerDao.insert(jcCustomer);
        return jcCustomer;
    }

    /**
     * 修改数据
     *
     * @param jcCustomer 实例对象
     * @return 实例对象
     */
    @Override
    public JcCustomer update(JcCustomer jcCustomer) {
        this.jcCustomerDao.update(jcCustomer);
        return this.queryById(jcCustomer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.jcCustomerDao.deleteById(id) > 0;
    }
}
